package com.snowfield.lbcommon.config;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.Properties;

/**
 * 读取项目相关配置
 *
 * @author snowfield
 */
@Component("snowfieldConfig2")
@ConfigurationProperties(prefix = "snowfield")
public class SnowFieldConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(SnowFieldConfig.class);
    /**
     * 项目名称
     */
    private String name;

    /**
     * 版本
     */
    private String version;

    /**
     * lb47系统基本数据文件路径
     */
    private static String basicDataPath;

    /**
     * lb47系统基本配置文件路径
     */
    private static String basicConfigPath;

    /**
     * 业务模块文件上传保存路径
     */
    private static String uploadPath;

    /**
     * 后端服务器证书保存路径
     */
    private static String serverCrtPath;

    /**
     * 后端代理签名证书保存路径
     */
    private static String proxyCrtPath;

    /**
     * haproxy配置文件保存路径
     */
    private static String haConfigPath;

    /**
     * keepalived配置文件保存路径
     */
    private static String kaConfigPath;

    /**
     * 网络接口配置文件目录
     * /etc/sysconfig/network-scripts/
     */
    private static String networkScriptsPath = "/etc/sysconfig/network-scripts";

    @PostConstruct
    public void init() {
        try {
            LOGGER.info("create directory {} if not exists", getBasicDataPath());
            File basicDataDir = new File(getBasicDataPath());
            if (!basicDataDir.exists() || !basicDataDir.isDirectory()) {
                basicDataDir.mkdirs();
            }

            LOGGER.info("create directory {} if not exists", getBasicConfigPath());
            File basicConfigDir = new File(getBasicConfigPath());
            if (!basicConfigDir.exists() || !basicConfigDir.isDirectory()) {
                basicConfigDir.mkdirs();
            }

            LOGGER.info("create directory {} if not exists", getUploadPath());
            File uploadPathDir = new File(getUploadPath());
            if (!uploadPathDir.exists() || !uploadPathDir.isDirectory()) {
                uploadPathDir.mkdirs();
            }

            LOGGER.info("create directory {} if not exists", getServerCrtPath());
            File serverCrtPathDir = new File(getServerCrtPath());
            if (!serverCrtPathDir.exists() || !serverCrtPathDir.isDirectory()) {
                serverCrtPathDir.mkdirs();
            }

            LOGGER.info("create directory {} if not exists", getProxyCrtPath());
            File proxyCrtPathDir = new File(getProxyCrtPath());
            if (!proxyCrtPathDir.exists() || !proxyCrtPathDir.isDirectory()) {
                proxyCrtPathDir.mkdirs();
            }

            LOGGER.info("create directory {} if not exists", getHaConfigPath());
            File haConfigPathDir = new File(getHaConfigPath());
            if (!haConfigPathDir.exists() || !haConfigPathDir.isDirectory()) {
                haConfigPathDir.mkdirs();
            }

            LOGGER.info("create directory {} if not exists", getKaConfigPath());
            File kaConfigPathDir = new File(getKaConfigPath());
            if (!kaConfigPathDir.exists() || !kaConfigPathDir.isDirectory()) {
                kaConfigPathDir.mkdirs();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public static String getBasicDataPath() {
        return basicDataPath;
    }

    public void setBasicDataPath(String basicDataPath) {
        SnowFieldConfig.basicDataPath = basicDataPath;
    }

    public static String getBasicConfigPath() {
        return basicConfigPath;
    }

    public void setBasicConfigPath(String basicConfigPath) {
        SnowFieldConfig.basicConfigPath = basicConfigPath;
    }

    public static String getServerCrtPath() {
        return serverCrtPath;
    }

    public void setServerCrtPath(String serverCrtPath) {
        this.serverCrtPath = serverCrtPath;
    }

    public static String getProxyCrtPath() {
        return proxyCrtPath;
    }

    public void setProxyCrtPath(String proxyCrtPath) {
        this.proxyCrtPath = proxyCrtPath;
    }

    public static String getUploadPath() {
        return uploadPath;
    }

    public void setUploadPath(String uploadPath) {
        this.uploadPath = uploadPath;
    }

    public static String getHaConfigPath() {
        return haConfigPath;
    }

    public void setHaConfigPath(String haConfigPath) {
        SnowFieldConfig.haConfigPath = haConfigPath;
    }

    public static String getKaConfigPath() {
        return kaConfigPath;
    }

    public void setKaConfigPath(String kaConfigPath) {
        SnowFieldConfig.kaConfigPath = kaConfigPath;
    }

    public static String getNetworkScriptsPath() {
        Properties props = System.getProperties();
        String osName = props.getProperty("os.name");
        if (StringUtils.isEmpty(osName) || osName.toLowerCase().contains("window")){
            return SnowFieldConfig.getBasicDataPath();
        }
        return networkScriptsPath;
    }

    public static void setNetworkScriptsPath(String networkScriptsPath) {
        SnowFieldConfig.networkScriptsPath = networkScriptsPath;
    }
}
